# Maintainer: ImperatorS79 <fevrier.simon@gmail.com>

_realname=amgcl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.4.3
pkgrel=1
pkgdesc="AMGCL is a header-only C++ library for solving large sparse linear systems with algebraic multigrid (AMG) method. (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='http://amgcl.readthedocs.io/'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-gcc-fortran")
             "${MINGW_PACKAGE_PREFIX}-scotch")
depends=("${MINGW_PACKAGE_PREFIX}-boost")
source=("https://github.com/ddemidov/amgcl/archive/refs/tags/${pkgver}.tar.gz")
sha256sums=('e920d5767814ce697d707d1f359a16c9b9eb79eba28fe19e14c18c2a505fe0ad')
            
prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
}        

build() {
  cd "${srcdir}"/${_realname}-${pkgver}
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"
  
  local fortran_config
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    fortran_config+=(
      -DAMGCL_HAVE_FORTRAN=ON
    )
  fi

  #Version of pastix too old in amgcl
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DAMGCL_BUILD_TESTS=ON \
      -DAMGCL_BUILD_EXAMPLES=ON \
      "${fortran_config[@]}" \
      -DAMGCL_HAVE_PYTHON=ON \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/share/amgcl/cmake/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
